package com.qh.yyxt.controller;

import com.qh.yyxt.entity.Borrow;
import com.qh.yyxt.pojo.entity.meeting.DelayBorrow;
import com.qh.yyxt.pojo.entity.meeting.User;
import com.qh.yyxt.pojo.BorrowDTO;
import com.qh.yyxt.pojo.BorrowPartDTO;
import com.qh.yyxt.service.BorrowService;
import com.qh.yyxt.service.DelayBorrowService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class BorrowController {
    @Autowired
    private BorrowService borrowService;

    @Autowired
    private DelayBorrowService delayBorrowService;
    @RequestMapping("/user/getOneDelay")
    @ResponseBody
    public BorrowDTO getOneDelay(int id)
    {
       return borrowService.findOneBorrow(id);
    }
    @RequestMapping("/user/submit")
    @ResponseBody
    public String submitBorrow(
            @RequestParam("account") String mid,
            @RequestParam("meetingname") String name,
            @RequestParam("meetingneirong") String text,
            @RequestParam("starttime") String startTime,
            @RequestParam("endtime") String endTime,
            @RequestParam("people") String num,
            @RequestParam("huatong") String micro_num,
            @RequestParam("teashu") String cup_num,
            @RequestParam("control") String control,
            @RequestParam("lingdao") String leader,
            @RequestParam("jingbanren") String handler,
            @RequestParam("fuzeren") String director,
            @RequestParam("phoneid") String tel,
            @RequestParam("gongan") String security,
            @RequestParam("wireless") String wireless,
            HttpSession session
    )
    {
        if(control=="否")
        {
            return "false";
        }
        if(security=="否")
        {
            return "false";
        }
        try {
            User user=(User) SecurityUtils.getSubject().getPrincipal();
            //System.out.println(user.getId());
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Borrow borrow = new Borrow();
            borrow.setMid(Integer.parseInt(mid));
            if(user!=null){
                borrow.setUid(user.getId());
                borrow.setName(name);
                borrow.setText(text);
                borrow.setStarttime(format.parse(startTime));
                borrow.setEndtime(format.parse(endTime));
                borrow.setNum(Integer.parseInt(num));
                borrow.setMicroNum(Integer.parseInt(micro_num));
                borrow.setCupNum(Integer.parseInt(cup_num));
                borrow.setWirelessNum(Integer.parseInt(wireless));
                borrow.setControl(control);
                borrow.setLeader(leader);
                borrow.setDirector(director);
                borrow.setTel(tel);
                borrow.setHandler(handler);
                borrow.setSecurity(security);
                borrow.setState("待审批");
                borrow.setTime(new Date());
            }else{
                return "添加失败 请重新登录！";
            }
            if(borrow.getStarttime().compareTo(borrow.getEndtime())==1)
            {
                return "失败，开始时间大于结束时间";
            }
            if(borrowService.findBorrowByMidAndTime(borrow.getMid(),borrow.getStarttime(),borrow.getEndtime())!=null)
            {
                return "请刷新查看会议室状态，提交的时间存在冲突！";
            }
            if(borrowService.addBorrow(borrow)==1)
            {
                return "true";
            }
        }catch (Exception e)
        {
            return "失败";
        }
        return "失败";
    }
    //用户撤回申请
    @RequestMapping("/user/cancelState")
    @ResponseBody
    public String cancelState(int borrowId)
    {
        Borrow borrow = null;
        borrow = borrowService.findById(borrowId);
        if (borrow != null)
        {
            if(borrow.getStarttime().compareTo(new Date())>=0&&((borrow.getState().equals("审批通过")==true)||(borrow.getState().equals("待审批")==true)))
            {
                if(borrowService.changeState(borrowId, "撤销待审批")==1)
                    return "true";
                else return "撤销请求提交失败！";
            }else
            {
                return "会议已开始或结束，撤销失败";
            }
        } else {
            return "撤销失败";
        }
    }
    //用户个人查看的borrow信息
    //不分页式
    @RequestMapping("/user/getBorrow")
    @ResponseBody
    public List<BorrowPartDTO> getBorrow() {

        User user=(User) SecurityUtils.getSubject().getPrincipal();
        int uid=user.getId();
        List<BorrowPartDTO> dtoList = borrowService.findByUid(uid);
        return dtoList;
    }
    //查看所有的审批通过的 包括历史
    @RequestMapping("/user/findAfterToday")
    @ResponseBody
    public List<BorrowPartDTO> findAfterToday()
    {
        return borrowService.findAfterToday();
    }
    //会议延时
    @RequestMapping("/user/delay")
    @ResponseBody
    public String delay(int id, String d)  {
        if(d==null)
            return "false";
        if(d=="")
            return "false";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date= null;
        try {
            date = format.parse(d);
        } catch (Exception e)
        {
            return "日期格式错误！";
        }
        Borrow b = null;
        b = borrowService.findById(id);
        if (b == null)
        {
            return "记录不存在，失败";
        }
        if(b.getState().equals("会议结束"))
        {
            return "会议结束，无法延时!";
        }
        if (borrowService.findBorrowByMidAndEndTime(id,date) == null) {
            //如果没冲突那么就可以申请延时
            DelayBorrow delayBorrow=new DelayBorrow();
            delayBorrow.setBid(id);
            delayBorrow.setDelaytime(date);
            delayBorrow.setDelaystate("待审批");
            if(delayBorrowService.add(delayBorrow)==1)
            {
                return "true";
            }else{
                return "延时失败";
            }
        } else {
            return "存在冲突，无法延时";
        }
    }
    //判断开始时间的选择
    @RequestMapping("/user/judgeStartTime")
    @ResponseBody
    public String judgeStartTime(int mid,String starttime) {

        if(starttime==null)
            return "false";
        if(starttime=="")
            return "false";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if(borrowService.findBorrowByMidAndStartTime(mid,format.parse(starttime))==null)
            {
                return "true";
            }else{
                return "false";
            }
        }catch (Exception e)
        {
            return "false";
        }
    }

    //同时判断开始结束时间 合不合适
    @RequestMapping("/user/judgeTime")
    @ResponseBody
    public String judgeTime(int mid,String starttime,String endtime)
    {

        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(borrowService.findBorrowByMidAndTime((mid),format.parse(starttime),  format.parse(endtime))==null)
            {
                return "true";
            }else{
                return "时间不合适，结束时间可能涉及其他会议或者您的开始和结束时间包含着其他会议";
            }
        } catch (ParseException e) {
            // e.printStackTrace();
            return "日期格式错误";
        }
    }
    @RequestMapping("/user/findDelay")
    @ResponseBody
    public DelayBorrow findDelay(int bid)
    {
        return delayBorrowService.getByBid(bid);
    }
}
